const Router = require("koa-router");
const requireDirectory = require("require-directory");

class InitManage {
  static InitCore(app) {
    InitManage.initLoadRouter(app);
    InitManage.initLoadHttpException()
    InitManage.loadConfig()
  }

  static loadConfig(path = '') {
    const configPath = path || process.cwd() + '/config/config.js'
    const config = require(configPath)
    global.config = config
}

  static initLoadRouter(app) {
    requireDirectory(module, `${process.cwd()}/app/api`, {
      visit: whenLoadModule,
    });

    function whenLoadModule(obj) {
      if (obj.router && obj.router instanceof Router) {
        app.use(obj.router.routes());
      } else if (obj instanceof Router) {
        app.use(obj.routes());
      }
    }
  }

  static initLoadHttpException() {
    global.errs = require("./http-exception");
  }
}
module.exports = InitManage;
